package com.ymatou.iapi.productmanager.testcase;

import java.math.BigDecimal;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.member.parameter.CreateMemberBean;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.productmanager.parameter.ProcessOrderCancelV2Bean;
import com.ymatou.iapi.productmanager.service.ProcessOrderCancelV2Call;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.GodMemberService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmember.MemberPoints;
import com.ymttest.database.model.godmember.PointsEntry;
import com.ymttest.database.sqlwapper.godmember.GodMemberWapper;
import com.ymttest.utils.TestDataManager;
/**
*接收订单取消消息Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ProcessOrderCancelV2{
	private static ProcessOrderCancelV2Bean processordercancelv2Bean;
	private static ProcessOrderCancelV2Call processordercancelv2Call;
	GodMemberWapper GodMemberWapper;
	private static Integer shopid ;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("接收订单取消消息Test");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Before
	public void caseUp() {
		processordercancelv2Bean=new ProcessOrderCancelV2Bean();
		processordercancelv2Call=new ProcessOrderCancelV2Call();
		GodMemberWapper=new com.ymttest.database.sqlwapper.godmember.GodMemberWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(processordercancelv2Call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ProcessOrderCancelV2_001() {
		Logger.start(true,"正确数据，成功");
		try {shopid= GodmchManageCallService.CreateSellerMch();
		Long orderid=Long.valueOf(TestDataManager.getRandomNum(10));
		GodMemberService.mchSetOpenPoints(shopid, true);
		GodMemberService.MchSetOpenPointsRule(GodMemberService.initMchSetOpenPointsRuleBean(shopid));
		UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
		int userId = DGUserCallService.UserCreateCall(usercreateBean);
		CreateMemberBean createMem = GodMemberService.initCreateMemberBean(shopid,userId,usercreateBean.getOpenId());
		String memid = GodMemberService.creatememberCall(createMem);
		String sqOrderRmbAmount="101";
		GodMemberService.processOrderPay(userId, shopid, orderid, sqOrderRmbAmount);
		processordercancelv2Bean.setShopId(shopid);
		processordercancelv2Bean.setUserId(userId);
		processordercancelv2Bean.setOrderId(orderid);
		processordercancelv2Bean.setOrderAmt(new BigDecimal(sqOrderRmbAmount));
		processordercancelv2Bean.setCancelAmount(new BigDecimal(sqOrderRmbAmount));
		processordercancelv2Call.setData(processordercancelv2Bean);
		processordercancelv2Call.callService();			
		Logger.verifyEquals("ok", processordercancelv2Call.getReturnData(), "验证返回");
		
		Thread.sleep(5000);
		MemberPoints mpdb = GodMemberWapper.selectMemberPointsByMemberId(memid).get(0);
		Logger.debug(FormatUtil.beanToGSONString(mpdb));
//		{"memberId":"HY20200521001322","shopId":500962122,"availablePoints":10,"totalPoints":10,"orderNum":0,"orderAmt":0.00,"lastMonthOrderAmt":0.00,"lastOrderAmt":0.00,"lastOrderId":0,"createTime":"2020-05-21T18:45:52","updateTime":"2020-05-21T18:45:52"}
		Logger.verifyEquals(0,mpdb.getAvailablePoints(), "验证getAvailablePoints");
		Logger.verifyEquals(shopid,mpdb.getShopId(), "验证getShopId");
		Logger.verifyEquals(0,mpdb.getTotalPoints(), "验证getTotalPoints");
		Logger.verifyNotNull(mpdb.getCreateTime(), "验证getCreateTime");
		List<PointsEntry> Entrylist = GodMemberWapper.selectPointsEntryByMemberid(memid);
		Logger.debug(FormatUtil.beanToGSONString(Entrylist));
		PointsEntry entrydb = Entrylist.stream().filter(x->{return x.getBizType().equals(110000);}).findFirst().get();
//		[{"entryId":63,"memberId":"HY20200521001326","bizType":200000,"bizId":"c62523d2d75e466493ef30c28e58873e","changePoints":10,"points":10,"pointsTime":"2020-05-21T18:50:54","operator":"主账号","remark":"test932","createTime":"2020-05-21T18:50:54","updateTime":"2020-05-21T18:50:54"}]
		Logger.verifyEquals(-5,entrydb.getChangePoints(), "验证ChangePoints");
		Logger.verifyEquals(0,entrydb.getPoints(),  "验证points");
		Logger.verifyEquals("系统",entrydb.getOperator(),  "验证Operator");
		Logger.verifyEquals("退款扣积分",entrydb.getRemark(), "验证remark");} catch (Exception e) {
			Logger.fail(e);
		}
	}
}